/* ***********************************************************
Example 6-4
"Dynamic HTML:The Definitive Reference"
by Danny Goodman
Published by O'Reilly & Associates  ISBN 1-56592-494-0
http://www.oreilly.com
Copyright 1998 Danny Goodman.  All Rights Reserved.
************************************************************ */

// Global variables for platform branching
var isNav, isIE
if (parseInt(navigator.appVersion) >= 4) {
	if (navigator.appName == "Netscape") {
		isNav = true
	} else {
		isIE = true
	}
}

// ***Begin Utility Functions***
// Set zIndex property
function setZIndex(obj, zOrder) {
	obj.zIndex = zOrder
}
// Position an object at a specific pixel coordinate
function shiftTo(obj, x, y) {
	if (isNav) {
		obj.moveTo(x,y)
	} else {
		obj.pixelLeft = x
		obj.pixelTop = y
	}
}
// ***End Utility Functions***

// Global holds reference to selected element
var selectedObj

// Find out which element has been clicked on
function setSelectedElem(evt) {
	if (isNav) {
		var clickX = evt.pageX
		var clickY = evt.pageY
		var testObj
		for (var i = document.layers.length - 1; i >= 0; i--) {
			testObj = document.layers[i]
			if ((clickX > testObj.left) && 
				(clickX < testObj.left + testObj.clip.width) && 
				(clickY > testObj.top) && 
				(clickY < testObj.top + testObj.clip.height)) {
				selectedObj = testObj
				if (selectedObj) {
					setZIndex(selectedObj, 100)
					return
				}
			}
		}
	} else {
		var imgObj = window.event.srcElement
		selectedObj = imgObj.parentElement.style
		if (selectedObj) {
			setZIndex(selectedObj,100)
			return
		}
	}
	selectedObj = null
	return
}

function twiddle(evt) {
	setSelectedElem(evt)
	if (selectedObj) {
		wx = selectedObj.clip.width;
		deltax = (Math.random() * wx * 4) - wx*2; 
		if (deltax >= 0 && deltax < wx) 
			deltax += wx;
		if (deltax < 0 && deltax > -wx) 
			deltax -= wx;
		wy = selectedObj.clip.height;
		deltay = (Math.random() * wy * 4) - wy*2; 
		if (deltay >= 0 && deltay < wy) 
			deltay += wy;
		if (deltay < 0 && deltay > -wy) 
			deltay -= wy;

		newx = deltax + selectedObj.left;
		if (newx < wx) 
			newx = wx - newx;
		if (newx > document.width - wx) 
			newx = 2 * (document.width - wx) - newx;
		newy = deltay + selectedObj.top;
		if (newy < wy) 
			newy = wy - newy;
		if (newy > document.height - wy) 
			newy = 2 * (document.height - wy) - newy;
		shiftTo(selectedObj, newx, newy);
		document.status = "Newx " + newx;
		setZIndex(selectedObj, 0)
		selectedObj = null
	}
}

// Assign event handlers 
function init() {
	if (isNav) {
		document.imgA.captureEvents(Event.MOUSEOUT | Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP)
		document.imgA.onmouseover = twiddle;
		document.imgA.onmouseout = twiddle;
		for (Z=0; Z<document.anchors.length; Z++) {
			//debug("Z = " + Z + "; name = " + document.anchors[Z].name);
			if (document.anchors[Z].name=="imgB") break;
		}
		if (Z==document.anchors.length)
			shiftTo(document.imgA,document.imgA.clip.width/2,document.height/4);
		else
			shiftTo(document.imgA,document.anchors[Z].x - document.imgA.clip.width/2,document.anchors[Z].y);
		document.imgA.hidden = false;
	} else {
		document.images[1].onmouseover = twiddle;
		document.images[1].onmouseout = twiddle;
		shiftTo(document.images[1], document.images[1].width/2, document.height/4);
		document.images[1].hidden = false;
	}
}
